#include<bits/stdc++.h>
using namespace std;
#define int long long

int p[100005] , cnt[100005];

int find(int x){
	if(p[x] == x) return x;
	p[x] = find(p[x]);
	return p[x];
}

void merge(int x , int y){
	x = find(x) , y = find(y);
	p[y] = x;
	cnt[x] += cnt[y];
	cnt[y] = 0;
}


int n;

signed main(){
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cnt[i] = 1;
		p[i] = i;
	}
	for(int i = 1 ; i <= n ; i++){
		char ch;
		cin >> ch;
		if(ch == 'M'){
			int x , y;
			cin >> x >> y;
			merge(y , x);
		}else{
			int z;
			cin >> z;
			cout << cnt[z] << endl;
		}
	}
	
	return 0;
}
